#!/usr/bin/env python
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt

arr = []

def init_data(input_file, from_num, to_num):
    global arr
    arr = []
    with open(input_file, 'r+') as f:
        for line in f:
            minus = int(line.strip().split('\t')[3])
            if minus >= from_num and minus <= to_num:
                arr.append(minus)
    f.close()
    print 'size of [%d, %d]: %d' % (from_num, to_num, len(arr))

def init_data_abs(input_file, size):
    global arr
    arr = []
    with open(input_file, 'r+') as f:
        for line in f:
            minus = int(line.strip().split('\t')[3])
            if abs(minus) <= size:
                arr.append(minus)
    f.close()
    print 'size of [%d, %d]: %d' % (0-size, size, len(arr))
    
def draw(input_file, from_num, to_num):
    global arr
    init_data(input_file, from_num, to_num)
    
    fig = plt.figure()
    n, bins, patches = plt.hist(arr)
#     plt.axis([0, 10000, 0, 350000])
    plt.title('Changes of PageRank Distribution in [%d, %d]' % (from_num, to_num))
    plt.xlabel('PageRank Changes')
    plt.ylabel('Count')
#     plt.show()
    fig.savefig('/home/ivy/git/BD_py/PageRank_py/project/resource/sort_all/distribution_%d_%d.png' % (from_num, to_num), \
            bbox_inches='tight')

def draw_abs(input_file, size):
    global arr
    init_data_abs(input_file, size)
    
    fig = plt.figure()
    n, bins, patches = plt.hist(arr)
#     plt.axis([0, 10000, 0, 350000])
    plt.title('Changes of PageRank Distribution in [%d, %d]' % (0-size, size))
    plt.xlabel('PageRank Changes')
    plt.ylabel('Count')
#     plt.show()
    fig.savefig('/home/ivy/git/BD_py/PageRank_py/project/resource/sort_all/distribution_%d.png' % size, \
            bbox_inches='tight')

def draw_top(input_file, top):
    arr = []
    counter = 0
    with open(input_file, 'r+') as f:
        for line in f:
            if counter > top:
                break
            minus = int(line.strip().split('\t')[3])
            arr.append(minus)
            counter += 1
    f.close()
    print 'max: %d, min: %d' % (int(max(arr)),int(min(arr)))
    fig = plt.figure()
    n, bins, patches = plt.hist(arr)
#     plt.axis([0, 10000, 0, 350000])
    plt.title('Changes of PageRank Distribution in Top %d' % top)
    plt.xlabel('PageRank Changes')
    plt.ylabel('Count')
    plt.show()
    fig.savefig('/home/ivy/git/BD_py/PageRank_py/project/resource/sort_all/distribution_top_%d.png' % top, \
            bbox_inches='tight')

def pie_chart():
    fig = plt.figure()
    labels = '[-100, 100]', '[-200000, 200000]', '[-1000, 1000]', '[-10000, 10000]'
    sizes = [1043, 751686-68323, 8629-1043, 68323-8629]
    colors = ['yellowgreen', 'lightcoral', 'gold', 'lightskyblue']
    explode = (0.4, 0.1, 0, 0.1)
    
    plt.pie(sizes, explode=explode, labels=labels, colors=colors,
            autopct='%1.1f%%', shadow=True, startangle=90)
    # Set aspect ratio to be equal so that pie is drawn as a circle.
    plt.axis('equal')
    
    fig.savefig('/home/ivy/git/BD_py/PageRank_py/project/resource/sort_all/pie_chart.png', \
            bbox_inches='tight')
    plt.show()
   
def main():
    
    input_file = '/home/ivy/git/BD_py/PageRank_py/project/resource/sort_all/index_old_new_minus.txt'
#     draw_abs(input_file, 100)
#     draw_abs(input_file, 1000)
#     draw_abs(input_file, 10000)
#     draw_abs(input_file, 200000)
#     draw(input_file, 0, 200000)

#     init_data_abs(input_file, 100)
#     init_data_abs(input_file, 1000)
#     init_data_abs(input_file, 10000)
#     init_data_abs(input_file, 200000)
#     init_data(input_file, 0, 200000)
    
#     draw_top(input_file, 100)
    
if __name__ == '__main__':
    main()